<?php declare(strict_types=1);


namespace App\Api\V2\Controller\Lite;


use App\Api\Controllers\BaseController;
use App\Services\PengYiPeng\SPILiteService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class SpiPayController extends BaseController
{

    //明日处理bind  支付  查询 退款  退款查询

    //开始支付
    public function pay(Request $request): string
    {
        $data = $request->all();
        $url = $request->fullUrl();
        Log::channel("notify-pay")->info("pay日志_data" . date("Y-m-d H:i:s", time()), $data);
        Log::channel("notify-pay")->info("pay日志_url" . date("Y-m-d H:i:s", time()) . $url);
        //发起支付代码
        //自定义参数unicode编码
        $data["terminal_bind_info"] = json_encode(json_decode($data["terminal_bind_info"]));
        //u
        $service = new SPILiteService();
        Log::channel("notify-pay")->info("handleSPI");
        try {
            $result = $service->paySPI($data);
            Log::channel("notify-pay")->info("handleSPIsuccess_pay" . date("Y-m-d H:i:s", time()), $result);
            return json_encode($result);
        } catch (\Exception $e) {
            Log::channel("notify-pay")->info("Exception" . date("Y-m-d H:i:s", time()), [$e->getMessage() . $e->getFile() . $e->getLine()]);
            return $e->getMessage() . $e->getFile() . $e->getLine();
        }
    }

    //支付查询
    public function payQuery(Request $request): string
    {

        $data = $request->all();
        $url = $request->fullUrl();
        Log::channel("notify-query")->info("query日志_data" . date("Y-m-d H:i:s", time()), $data);
        Log::channel("notify-query")->info("query日志_url" . date("Y-m-d H:i:s", time()) . $url);
        //发起支付代码

        $data["terminal_bind_info"] = json_encode(json_decode($data["terminal_bind_info"]));

        $service = new SPILiteService();
        Log::channel("notify-query")->info("handleSPI");
        try {
            $result = $service->payQuerySPI($data);
            Log::channel("notify-query")->info("handleSPIsuccess_payQuery:" . ($result["isv_order_no"]??"" ). "—时间：" . date("Y-m-d H:i:s", time()), $result);
            return json_encode($result);
        } catch (\Exception $e) {
            Log::channel("notify-query")->info("Exception" . date("Y-m-d H:i:s", time()), [$e->getMessage() . $e->getFile() . $e->getLine()]);
            return $e->getMessage() . $e->getFile() . $e->getLine();
        }
    }


}